/*
 * GraphicalUtils.java
 *
 * Created on 13 maj 2007, 17:53
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package pl.krakow.ae.knp.wikiboard.utils;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import pl.krakow.ae.knp.wikiboard.gui.GUI;

/**
 *
 * Set of utility methods.
 */
public class GraphicalUtils
{
    /* Returns a deep copy of a BufferedImage object.
     * @param bi an object to be cloned.
     * @return a cloned object or null if a parameter is null.
     */
     
    public static BufferedImage cloneBufferedImage(BufferedImage bi)
    {
        if (bi == null)
        {
            System.err.println("Passed null parameter");
            return null;
        }
            
        BufferedImage result = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
        result.setData(bi.getData());
        return result;
    }
    
    /** Returns an ImageIcon, or null if the path was invalid. 
     * @param path a path to the image
     * @return an ImageIcon object or null if the path was invalid
     */
    
    public static ImageIcon createImageIcon(String path) 
    {
        java.net.URL imgURL = GUI.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
            
        }
    }
    
    /** Returns a deep copy of a Color.
     * @param original Color instance to be cloned.
     * @return a deep copy of original.
     */
    public static Color cloneColor(Color original)
    {
        if (original == null)
            return null;
        
        int r = original.getRed();
        int g = original.getGreen();
        int b = original.getBlue();
        int a = original.getAlpha();
        
        return new Color(r, g, b, a);
    }

    /** Generates a transparent Color in ARGB colorspace.
     * @param color an original color in RGB colorspace.
     * @return a transparent equivalent in ARGB colorspace. */
    public static Color createTransparentColor(Color color)
    {
        int r = color.getRed();
        int g = color.getGreen();
        int b = color.getBlue();
        return new Color(r, g, b, 0);
    }
}
